<!-- 个人中心 -->
<template>
	<view class="personal-center page-background">
		<view class="center-top">
			<view class="main-content flex-box">
				<!-- 登录框遮罩 -->
				<view v-if="!userInfo.id" class="login-view">
					<xjx-login
						:recUser="recUser"
						@callBack="loginCallBack"
						:loginToEmit="true"
						ref='loginCom'
					>
						<template v-slot:loginBtn>
							<view class="login-view-area"></view>
						</template>
					</xjx-login>
				</view>
				<view v-if="userInfo.id" class="left">
					<image v-if="userInfo.icon" :src="userInfo.icon" mode=""></image>
					<image v-else :src="'icon_renshu.png'|addBaseImgUrl" mode=""></image>
				</view>
				<view v-else class="login-image">
					<image :src="'icon_renshu.png'|addBaseImgUrl" mode=""></image>	
				</view>
				<view class="right">
					<view class="row1 flex-box">
						<view v-if="userInfo.id" class="name">
							{{userInfo.userName}}
						</view>
						<view v-else class="login-btn">
							登录/注册
						</view>
					</view>
					<view class="row2">
						社区帮购 帮你购
					</view>
				</view>
			</view>
		</view>
		<view class="item-box center-middle">
			<view class="flex-box row1">
				<text class="title">我的订单</text>
				<view @click="to('/personalCenter/myOrder/index/index?type=0')" class="order-more">
					<text>全部订单</text>
					<van-icon name="arrow" color="#999999" size="14px"/>
				</view>
			</view>
			<view class="flex-box row2">
				<view @click="to('/personalCenter/myOrder/index/index?type=1')" class="row2-item">
					<view class="image-box">
						<image class="need-pay" :src="'@/static/images/mine/icon-nopay.png'|addBaseImgUrl" mode=""></image>
						<text v-if="orderCount.nopay&&orderCount.nopay>0">{{orderCount.nopay}}</text>
					</view>
					<view class="text">
						待付款
					</view>
				</view>
				<view @click="to('/personalCenter/myOrder/index/index?type=2')" class="row2-item">
					<view class="image-box">
						<image class="ready-pay" :src="'@/static/images/mine/icon-nouse.png'|addBaseImgUrl" mode=""></image>
					</view>
					<view class="text">
						待使用
					</view>
				</view>
				<view @click="to('/personalCenter/myOrder/index/index?type=3')" class="row2-item">
					<view class="image-box">
						<image class="need-eval" :src="'@/static/images/mine/icon-noevaluate.png'|addBaseImgUrl" mode=""></image>
						<text v-if="orderCount.nopraised&&orderCount.nopraised>0">{{orderCount.nopraised}}</text>
					</view>
					<view class="text">
						待评价
					</view>
				</view>
				<view @click="to('/personalCenter/myOrder/index/index?type=4')" class="row2-item">
					<view class="image-box">
						<image class="refund" :src="'@/static/images/mine/icon-refund.png'|addBaseImgUrl" mode=""></image>
					</view>
					<view class="text">
						退款/售后
					</view>
				</view>
			</view>
		</view>
		<view class="item-box center-bottom">
			<view class="list-row flex-box">
				<view @click="to('/personalCenter/myCourse/index/index')" class="flex-item">
					<view class="image-box">
						<image class="image-course" :src="'@/static/images/mine/icon-course.png'|addBaseImgUrl" mode=""></image>
					</view>
					<view class="text">
						我的课程
					</view>
				</view>
			</view>
			<view class="list-column">
				<view @click="to('/personalCenter/invite/invite?id=1')" class="list-item flex-box">
					<image :src="'@/static/images/mine/icon-gift.png'|addBaseImgUrl" mode=""></image>
					<view class="item-title">
						邀请好友
					</view>
					<van-icon name="arrow" color="#999999" size="16px"/>
				</view>
				<view @click="to('/personalCenter/about/about')" class="list-item flex-box">
					<image :src="'@/static/images/mine/icon-about.png'|addBaseImgUrl" mode=""></image>
					<view class="item-title">
						关于我们
					</view>
					<van-icon name="arrow" color="#999999" size="16px"/>
				</view>
			</view>
			<view v-if="accessToken" class="btn-box">
				<view @click="toLoginOut" class="login-out-btn">
					退出登录
				</view>
			</view>
			<view v-if="userInfo.id" class="get-phone-box">
				<button class="get-phone-btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">获取手机号</button>
			</view>
		</view>
		
		<!-- 弹出框 -->
		<van-dialog id="van-dialog" confirm-button-color="#fe925c" cancel-button-color="#fe925c" />
		<!-- 下拉选择 -->
		<van-popup :show="popupShow"
			position="bottom"
			round
			closeable
			@close="popupClose"
			custom-style="height:485px;">
			<view class="popup-view">
				<image class="popup-logo" :src="'../../../static/images/icon-logo.svg'|addBaseImgUrl" mode=""></image>
				<view class="popup-tips">申请获得你微信绑定的手机号</view>
				<button class="popup-btn" open-type="getPhoneNumber" @getphonenumber="getWxPhoneNumber">获取手机号</button>
			</view>
		</van-popup>
	</view>
</template>

<script>
	import { mapState, mapGetters, mapMutations } from 'vuex'
	import Dialog from '@/wxcomponents/vant/dist/dialog/dialog'
	export default {
		data() {
			return {
				wxPhoneNumberInfo: null,
				popupShow: false,
				orderCount: {
					nopay: 0,
					nopraised: 0,
					refund: 0
				},
				recUser: ''
			}
		},
		computed: {
			...mapState(['userInfo','accessToken']),
		},
		onLoad(data){
			this.recUser = data.recUser || ''
			if(!this.accessToken&&this.recUser){
				uni.showToast({
					title: '欢迎加入八爪鱼',
					icon: 'none'
				})
			}
		},
		onShow(){
      wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: '#fe925c'
        });
			let aa = uni.getStorageSync('needLogin')
			if(aa){
				uni.removeStorageSync('needLogin')
				this.loginOut()
				this.emptyOrderCount()
				uni.showToast({
					title: '请先登录~',
					duration: 3000,
					icon: 'none'
				})
			}
		},
		methods: {
			...mapMutations(['setStateAttr','loginOut']),
			to(url){
				if(!this.accessToken){
					this.$refs.loginCom.getWxUserInfoNew()
					return false
				}
				uni.navigateTo({
					url: url
				})
			},
			emptyOrderCount(){
				this.orderCount = {
					nopay: 0,
					nopraised: 0,
					refund: 0
				}
			},
			toLoginOut(){
				Dialog.confirm({
					title: '这么多好玩的课程活动，确定不再看看吗？',
					cancelButtonText: '取消',
					width: '325px',
					messageAlign: 'left',
					customStyle: 'border-radius:7.5px;',
					className: 'my-dialog-confirm'
				})
				.then(() => {
					this.loginOut()
					this.emptyOrderCount()
				})
				.catch(() => {
				})
				
			},
			// 登录完成回调
			loginCallBack(){
				if(this.recUser){
					this.recUser = ''
				}
				if(uni.getStorageSync('loginToPage')){
					let url = uni.getStorageSync('loginToPage')
					uni.removeStorageSync('loginToPage')
					uni.navigateTo({
						url: url
					})
				}
			},
			popupClose(){
				this.popupShow = false
			},
			onGetPhoneNumber(e) {
				if (e.detail.errMsg === 'getPhoneNumber:ok') {
					const { encryptedData, iv } = e.detail;
					wx.cloud.callFunction({
					name: 'getPhoneNumber',
					data: {
						encryptedData,
						iv
					},
					success: res => {
						if (res.result.success) {
						this.setData({
							wxPhoneNumberInfo: res.result.data.phoneNumber,
							userInfo: res.result.data // 更新用户信息
						});
						wx.showToast({
							title: '获取成功',
							icon: 'success'
						});
						} else {
						wx.showToast({
							title: '获取失败',
							icon: 'none'
						});
						}
					},
					fail: err => {
						console.error('调用失败', err);
						wx.showToast({
						title: '调用失败',
						icon: 'none'
						});
					}
					});
				}
			}
		},
		watch: {
			userInfo:{
				handler(newValue, oldValue) {
					if(newValue.id){
					}
				},
				// immediate: true,
				deep: true
			}
		},
	}
</script>

<style lang="scss" scoped>
	.personal-center{
		min-height: 100vh;
		.center-top{
			height: 430rpx;
			background: linear-gradient(to bottom, #fe925c 0%, #fe925c 80%, #F2F2F2 100%);
			.main-content{
				padding: 20rpx 40rpx;
				box-sizing: border-box;
				align-items: center;
				position: relative;
				.login-view{
					position: absolute;
					// width: 370rpx;
					width: 670rpx;
					height: 170rpx;
					left: 40rpx;
					top: 22rpx;
					z-index: 97;
					.login-view-area{
						width: 370rpx;
						height: 170rpx;
					}
				}
				.left,.login-image{
					border: 10rpx solid rgba(255,255,255,.15);
					padding: 0;
					flex: 0 0 auto;
					width: 173rpx;
					height: 173rpx;
					border-radius: 50%;
					overflow: hidden;
					margin-right: 20rpx;
					background-color: tranparent;
				}
				.right{
					flex: 1 0 auto;
					position: relative;
					.row1{
						align-items: flex-end;
						.name,.login-btn{
							font-size: 34rpx;
							color: $text-color-white1;
							height: 56rpx;
							line-height: 56rpx;
							max-width: 240rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}
						.login-btn{
							background-color: transparent;
							margin: 0;
							padding: 0;
							border-color: white;
							border-radius: none;
							&:after{
								border: none;
							}
						}
						.icon-box{
							width: 35rpx;
							height: 34rpx;
							position: relative;
							image{
								width: 100%;
								height: 100%;
								vertical-align: middle;
							}
							.red-bit{
								width: 14rpx;
								height: 14rpx;
								position: absolute;
								background-color: #F77350;
								border-radius: 50%;
								top: 0;
								right: 0;
							}
						}
					}
					.row2{
						font-size: 24rpx;
						color: #B2D3FF;
						line-height: 42rpx;
						margin-bottom: 10rpx;
					}
					.row3{
						position: relative;
						width: 478rpx;
						height: 66rpx;
						align-items: center;
						border-top: 1rpx solid #fe925c;
						padding-top: 20rpx;
						padding-right: 15rpx;
						view{
							align-items: center;
						}
						.my-integral{
							// position: absolute;
							// left: 0;
							// bottom: 0;
							image{
								width: 39rpx;
								height: 36rpx;
							}
						}
						.my-vip{
							// position: absolute;
							// right: 0;
							// bottom: 0;
							image{
								width: 40rpx;
								height: 32rpx;
							}
						}
						text{
							font-size: 28rpx;
							color: #FFD2C2;
							margin-left: 20rpx;
						}
					}
					.right-btn{
						width: 230rpx;
						height: 78rpx;
						border: 2rpx solid #81B6FD;
						border-radius: 39rpx;
						line-height: 30rpx;
						color: white;
						align-items: center;
						justify-content: center;
						position: absolute;
						right: 0;
						top: 50%;
						margin-top: -39rpx;
						image{
							width: 34rpx;
							height: 27rpx;
							margin-right: 15rpx;
						}
					}
				}
			}
		}
		
		.center-middle{
			margin-top: -190rpx;
			height: 240rpx;
			padding: 20rpx;
			box-sizing: border-box;
			.row1{
				padding-left: 10rpx;
				.title{
					font-size: 32rpx;
					color: $text-color-dark-grey;
				}
				.order-more{
					color: $text-color-grey;
					font-size: 28rpx;
					display: flex;
					align-items: center;
					text{
						margin-right: 10rpx;
					}
				}
			}
			.row2{
				padding: 0 10rpx;
				margin-top: 35rpx;
				.row2-item{
					height: 110rpx;
					display: flex;
					flex-flow: column;
					justify-content: space-between;
					align-items: center;
					flex-basis: 140rpx;
					.image-box{
						position: relative;
						.need-pay{
							width: 43rpx;
							height: 46rpx;
						}
						.ready-pay{
							width: 44rpx;
							height: 46rpx;
						}
						.need-eval{
							width: 46rpx;
							height: 43rpx;
						}
						.refund{
							width: 39rpx;
							height: 46rpx;
						}
						text{
							background-color: #F77350;
							border-radius: 15rpx;
							height: 30rpx;
							position: absolute;
							right: 0;
							transform: translateX(50%);
							top: -15rpx;
							font-size: 26rpx;
							line-height: 30rpx;
							min-width: 30rpx;
							text-align: center;
							color: $text-color-white1;
						}
					}
					.text{
						font-size: 30rpx;
						color: $text-color-dark-grey;
					}
				}
			}
		}
		.center-bottom{
			padding: 30rpx 20rpx 0;
			margin-top: 15rpx;
			.list-row{
				.flex-item{
					height: 200rpx;
					background-color: #F8F8F8;
					border-radius: 15rpx;
					flex: 0 0 auto;
					width: 31%;
					display: flex;
					flex-flow: column;
					align-items: center;
					justify-content: center;
					.image-box{
						width: 90rpx;
						height: 90rpx;
						border-radius: 50%;
						background-color: white;
						display: flex;
						margin-bottom: 15rpx;
						align-items: center;
						justify-content: center;
						.image-course{
							width: 37rpx;
							height: 45rpx;
						}
						.image-act{
							width: 40rpx;
							height: 44rpx;
						}
						.image-club{
							width: 47rpx;
							height: 46rpx;
						}
					}
					.text{
						font-size: 30rpx;
						color: $text-color-dark-grey;
					}
				}
			}
			.list-column{
				margin-top: 5rpx;
				.list-item{
					height: 120rpx;
					align-items: center;
					box-sizing: border-box;
					padding-left: 10rpx;
					&+.list-item{
						border-top: 1rpx solid #E9E9E9;
					}
					image{
						width: 46rpx;
						height: 46rpx;
					}
					.item-title{
						color: $text-color-dark-grey;
						font-size: 30rpx;
						flex: 1 0 auto;
						margin-left: 30rpx;
					}
				}
			}
			.btn-box{
				width: 690rpx;
				padding: 40rpx 0 40rpx;
				border-top: 1rpx solid #E9E9E9;
				.login-out-btn{
					width: 690rpx;
					height: 88rpx;
					border-radius: 44rpx;
					line-height: 84rpx;
					border: 2rpx solid #E9E9E9;
					text-align: center;
					width: 100%;
					color: $text-color-dark-grey;
				}
			}
			.get-phone-box{
				width: 690rpx;
				padding: 40rpx 0 40rpx;
				border-top: 1rpx solid #E9E9E9;
				.get-phone-btn{
					width: 690rpx;
					height: 88rpx;
					border-radius: 44rpx;
					line-height: 84rpx;
					border: 2rpx solid #E9E9E9;
					text-align: center;
					width: 100%;
					color: $text-color-dark-grey;
				}
			}
		}
	}
</style>

<style lang="scss">
	
</style>
